import api from "../../Sendingmethod/Sendingmethod"
// pages/Login/Login.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        //是否输入完整
        inpublen: false,
        //账号
        account: '',
        //密码
        password: '',
        checked: false,
        nihei: false,
        titleobj: {
            "-1": '审核被拒',
            "0": "待审核",
            "1": "审核中",
            "2": "正常",
            "3": "已停用"
        },
        inputtype: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (wx.getStorageSync('userinfo')) {
            let info = JSON.parse(wx.getStorageSync('userinfo'))
            console.log(info);
            if (wx.getStorageSync('token')) {
                this.onlogininfo()
            } else {
                // console.log('跳转登录');
                // wx.redirectTo({
                //     url: '/pages/Login/Login',
                // })
            }
        }
    },
    /**
     * 切换用户type
     */
    qihuan() {
        console.log('开启');
        this.setData({
            inputtype: !this.data.inputtype
        })
    },
    qingchu() {
        this.setData({
            account: ''
        })
    },
    /**
     * 登录事件
     */
    loginfn() {
        console.log('点击登录', this.data.account, this.data.password);

    },
    /**
     * 登录
     */
    async Login(data) {
        console.log(data, '传输数据');
        wx.showLoading({
            title: '登陆中',
            mask: true
        })
        try {
            let res = await api.retuserlogin({
                username: this.data.account,
                password: this.data.password
            })

            console.log('登录返回信息', res);
            console.log(res);
            if (res.code == 0) {
                console.log('登录成功', res.data);
                let userdata = JSON.stringify({
                    username: this.data.account,
                    password: this.data.password
                })
                // 存储信息
                wx.setStorageSync('userdata', userdata)
                wx.setStorageSync("token", res.data.token)
                wx.setStorageSync("userid", res.data.uid)
                let userinfo = JSON.stringify(res.data.userinfo)
                wx.setStorageSync("userinfo", userinfo)
                console.log(wx.getStorageSync('userinfo'), '当前存储的');
                wx.hideLoading()

                if (res.data.userinfo.status == 0) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
                if (res.data.userinfo.status == 1) {
                    wx.showToast({
                        title: '该账号在审核中',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 3) {
                    wx.showToast({
                        title: '该账号已停用',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 2) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.switchTab({
                        url: '/pages/orders/orders',
                    })
                }
                if (res.data.userinfo.status == -1) {
                    console.log('11111');
                    wx.showToast({
                        title: '审核未通过',
                        icon: 'loading',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        } catch (error) {
            wx.showToast({
                title: '请求失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 自动登录
     */
    async onlogininfo() {

        try {
            let res = await api.userauthorize({})
            console.log('自动登录返回信息', res);
            console.log(res);
            if (res.code == 0) {
                console.log('登录成功', res.data);
                // 存储信息
                wx.setStorageSync("token", res.data.token)
                wx.setStorageSync("userid", res.data.uid)
                let userinfo = JSON.stringify(res.data.userinfo)
                wx.setStorageSync("userinfo", userinfo)
                console.log(wx.getStorageSync('userinfo'), '当前存储的');
                wx.hideLoading()
                if (res.data.userinfo.status == 0) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
                if (res.data.userinfo.status == 1) {
                    wx.showToast({
                        title: '该账号在审核中',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 3) {
                    wx.showToast({
                        title: '该账号已停用',
                        icon: 'loading',
                        duration: 2000
                    })
                }
                if (res.data.userinfo.status == 2) {
                    wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 2000
                    })
                    wx.switchTab({
                        url: '/pages/orders/orders',
                    })
                }
                if (res.data.userinfo.status == -1) {
                    console.log('11111');
                    wx.showToast({
                        title: '审核未通过',
                        icon: 'loading',
                        duration: 2000
                    })
                    wx.navigateTo({
                        url: '/pages/personal/personal',
                    })
                }
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        } catch (error) {
            wx.showToast({
                title: '请求失败',
                icon: 'error',
                duration: 2000
            })
        }
    },
    /**
     * 输入事件
     */
    inputfn(e) {
        if (e?.target.dataset.index) {
            console.log(e?.target.dataset.index, e.detail.value, '输入事件');
            if (e.target.dataset.index == '1') {
                console.log('触发账号');
                this.setData({
                    account: e.detail.value
                })
            }
            if (e.target.dataset.index == '2') {
                this.setData({
                    password: e.detail.value
                })
            }
        }


        if (this.data.account !== '' && this.data.password !== '') {
            console.log('触发验证');
            this.setData({
                inpublen: true
            })
        } else {
            this.setData({
                inpublen: false
            })
        }
    },
    /**
     * 登录按钮
     */
    loginbtn() {
        if (!this.data.checked) {
            this.setData({
                nihei: true
            })
        }
    },
    onChange() {
        let _that = this
        this.setData({
            checked: !_that.data.checked,
            nihei: false
        })
    },
    // 手机授权登录
    getPhoneNumber(e) {
        // 获取手机号码加密
        console.log(e, '获取手机号码加密');
        if (e.detail.code) {
            wx.showLoading({
                title: '登录中',
                mask: true
            })
            this.Login({
                code: e.detail.code
            })
        }
    },
    /**
     * 跳转事件
     */
    gotoa(e) {
        console.log(e.target.dataset.index);
        if (e.target.dataset.index == 'rtd') {
            wx.navigateTo({
                url: '/pages/registered/registered',
            })
        }
        if (e.target.dataset.index == 'Forgot') {
            wx.navigateTo({
                url: '/pages/Forgot/Forgot',
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (wx.getStorageSync('userdata')) {
            let obj = wx.getStorageSync('userdata')
            let data = JSON.parse(obj)
            this.setData({
                account: data.username,
                password: data.password
            })
            this.inputfn()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})